package com.petrochina.cloudconfig.util;

import com.alibaba.fastjson.JSON;
import com.petrochina.cloudconfig.domain.DeployServer;
import com.petrochina.cloudconfig.domain.Env;
import com.petrochina.cloudconfig.domain.Pkg;
import com.petrochina.cloudconfig.domain.env.EnvDeploy;
import com.petrochina.cloudconfig.domain.env.EnvSetting;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * env setting util
 */
public class EnvSettingUtil {


    private static String defaultFilePath = "/tmp";




    public static EnvSetting getEnvSetting(String envRootPath, String pkgFullName,String envName){

        //getDev config and set
        File envFile = getEnvFile(envRootPath, pkgFullName, envName);
        if(envFile.exists()){
            return getEnvSetting(envFile.toPath().toString());

        }else{
            return null;
        }

    }


    /**
     * get env setting obj from  a json file
     * @param filePath
     * @return
     */
    public static EnvSetting getEnvSetting(String filePath){
        EnvSetting envSetting = null;
        try {
            byte[] bytes = Files.readAllBytes(FileSystems.getDefault().getPath(filePath));
            envSetting = JSON.parseObject(new String(bytes, "gbk"), EnvSetting.class);
        } catch (IOException e) {
            //TODO file error
            e.printStackTrace();
        }

        return envSetting;


    }

    /**
     * getDev env file
     * @param envRootPath
     * @param pkgFullName
     * @param envName
     * @return
     */
    public static File getEnvFile(String envRootPath,String pkgFullName, String envName) {
        Pkg pkg = PkgUtil.getPkg(pkgFullName);
        if (pkg != null) {
            String filePath = PkgUtil.pkgToPath(pkg);
            //env file name
            String fileName = getEnvFileName(envName);
            //new file
            return new File(envRootPath + File.separator + filePath + File.separator + fileName);
        } else {
            return null;
        }
    }

    /**
     *get env file name e.g. dev.json
     * @param envName dev
     * @return env file name
     */
    public static String getEnvFileName(String envName){
        return envName+".json";

    }




    public static List<String> getIPs(EnvSetting envSetting){
        List<String> ips = new ArrayList<>();
        List<EnvDeploy> deploys = envSetting.getDeploys();
        if(deploys!=null && deploys.size()>0){
            for (int i = 0; i < deploys.size(); i++) {
                ips.addAll(deploys.get(i).getServers());
            }
        }
        return  ips;

    }

    /**
     * set env configs to env obj
     * @param env
     * @param envSetting
     */
    public static void setEnvConfigs(Env env, EnvSetting envSetting) {
        env.setEnvConfigs(envSetting.getConfigs());

    }
    /**
     * set env deploys to env obj
     * @param env
     * @param envSetting
     * @param deployServerMap
     */
    public static void setEnvDeploys(Env env, EnvSetting envSetting, Map<String, DeployServer> deployServerMap){
        List<EnvDeploy> deploys = envSetting.getDeploys();
        if(deploys != null && deploys.size()>0){
            List<DeployServer> deployServers = new ArrayList<>();
            for (int i = 0; i <deploys.size() ; i++) {
                EnvDeploy envDeploy = deploys.get(i);
                String path = envDeploy.getPath();
                if(path!=null && path.length()>0){
                    path=defaultFilePath;
                }

                List<String> servers = envDeploy.getServers();
                if(servers!=null && servers.size()>0){
                    for (int j = 0; j < servers.size(); j++) {
                        DeployServer deployServer = deployServerMap.get(servers.get(i));
                        if(deployServer != null){
                            deployServer.setDeployPath(path);
                            deployServers.add(deployServer);
                        }else{
                            //TODO write log
                        }
                    }
                }

            }
            env.setDeployServers(deployServers);
        }
    }
}
